package bag.servlet;

import bag.domain.Product;
import bag.service.CartService;
import bag.service.impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 冷丶 on 2017/10/24.
 */
@WebServlet(name = "AddCartServlet",urlPatterns = "/addCartServlet")
public class AddCartServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        CartService service = new CartServiceImpl();
        Product product = service.findById(pid);
        HttpSession session = request.getSession(true);
        List<Product> list = null;
        list = (List<Product>) session.getAttribute("cart");
        if(list == null){
            list = new ArrayList();
            session.setAttribute("cart",list);
        }
        list.add(product);
        response.sendRedirect("cartlist.jsp");
    }
}
